/**
 * @author monirul
 */

var RoomListView = new Class.create(
		{

			initialize : function() {

			},

			init : function(o) {
				if (o['mainComponentId'] != null)
					this._mainComponentId = o['mainComponentId'];
				// if(o['versionNo'] != null) this._versionNo = o['versionNo'];
				this._versionNo = 0;
				if (o['model'] != null)
					this._model = o['model'];
				if (o['controller'] != null)
					this._controller = o['controller'];
				if (o['elements'] != null) {
					this._elements = o['elements'];
					this._targetLocation = this._elements['targetLocation'];
				}

				var init = {};
				this.roomHistoryModel = ComponentManager.loadComponent( {
					componentId : 'roomHistoryModel_instance',
					jsClassName : 'RoomHistoryModel',
					initData : init
				});

				var roomHistoryViewElements = {
					'targetLocation' : 'history'
				};
				var init = {
					'model' : this.roomHistoryModel,
					'controller' : '',
					'elements' : roomHistoryViewElements
				};
				this.roomHistoryView = ComponentManager.loadComponent( {
					componentId : 'roomHistoryView_instance',
					jsClassName : 'RoomHistoryView',
					initData : init
				});
				var _this = this;
				// attach model listeners
				this._model.roomListUpdated.attach(function() {
					_this.rebuildList();
				});

			},
			show : function() {
				this.rebuildList();
			},

			setVersionNo : function(versionNo) {
				this._versionNo = versionNo;
			},

			errorHandlerOfJoin : function(name, compId, verNo) {
				alert("Join Room maybe timeout, now will try again");
				HostServiceCenter
						.join(
								name,
								compId,
								verNo,
								{
									callback : function() {
									},
									errorHandler : function(msg, ex) {
										alert("Error happened, Please login out and login again");
									}
								});
			},
			joinRoom : function(name, compId, verNo) {
				var roomHandler = this;
				HostServiceCenter.join(name, compId, verNo, {
					callback : function() {
					},
					errorHandler : roomHandler.errorHandlerOfOnLoad
							.bind(roomHandler)
				});
			},

			rebuildList : function() {
				// this.overlay1 = new YAHOO.widget.Overlay("history", {
				// fixedcenter:true,
			// visible:false,
			// width:"300px" } );
			// this.overlay1.render();
			// this.overlay1.show();

			this.rooms = new Array();
			this.height = 0;
			var roomList = this._model.getRoomList();
			this.isJoinedRoom = this._model.isJoined();
			this.canJoinRoom = true;
			for ( var i = 0; i < roomList.length; i++) {
				this.noOfUsers = 0;
				this.disbaled = " ";
				// if (roomList[i].userList != null
				// && roomList[i].userList.length > 0) {
				// this.noOfUsers = roomList[i].userList.length;
				// }
				this.noOfUsers = roomList[i].playerCount;
				// this.gameStateMsg = roomList[i].roomState;
				var roomStatus = "Open";
				if (roomList[i].state == 9) {
					roomStatus = "Ready To Start";
					this.canJoinRoom = true;
				} else if (roomList[i].state == 10) {
					roomStatus = "User Over";
					this.canJoinRoom = false;
				} else if (roomList[i].state == 1) {
					roomStatus = "Started";
					this.canJoinRoom = false;
				} else {
					this.canJoinRoom = true;
				}

				// if (roomList[i].gameState > 0) {
				// this.gameJoinOption = "disabled"
				// this.roomState = "Started";
				// this.disbaled = " disabled ";
				// } else {
				// this.roomState = "Waiting";
				// }
				if (this.room != null && this.room.isFreeUser) {
					this.canJoinRoom = false;
					this.disbaled = "  ";
				}
				if (this.isJoinedRoom) {
					this.disbaled = " disabled ";
				} else {
					this.disbaled = "  ";
					if (!this.canJoinRoom) {
						this.disbaled = " disabled ";
					}
				}

				this.roomLink = '<a  onclick="alert(\'room\');HistoryService.getRoomHistory(\''
						+ roomList[i].name
						+ '\',\'roomHistoryModel_instance\')" >'
						+ roomList[i].name + '</a>'

				// this.roomLink = '<a onclick="alert(\'sss\')"
				// >'+roomList[i].roomName+'</a>'
				this.joinLink = '<input type="button" value="join" '
						+ this.disbaled + ' onclick="HostServiceCenter.join(\''
						+ roomList[i].name + '\',\'' + this._mainComponentId
						+ '\',' + this._versionNo + ')"/>';
				// this.rooms[i] = {'roomName':roomList[i].roomName
				// ,'noOfUsers':this.noOfUsers
				// ,'state':roomList[i].roomState,'join':this.joinLink};

				this.rooms[i] = {
					'roomName' : this.roomLink,
					'noOfUsers' : this.noOfUsers,
					'state' : roomStatus,
					'join' : this.joinLink
				};
				this.height += 25;
			}
			this.disbaled = "";
			this.columnDefs = [ {
				key : "roomName",
				label : "Room Name",
				sortable : true
//				,
//				resizeable : true,
//				width : 60
			}, {
				key : "noOfUsers",
				label : "Users",
				sortable : true
//				,
//				resizeable : true,
//				width : 40
			}, {
				key : "state",
				label : "State"
//					,
//				resizeable : true,
//				width : 80
			}, {
				key : "join",
				label : ""
//					,
//				width : 30
			} ];

			var init = {
				'colDef' : this.columnDefs,
				'title' : "Room List",
				'targetPosition' : 'roomlist'
			};
			
			

			
			
			var roomListTbl = ComponentManager.loadComponent( {
				componentId : 'roomlist_table',
				jsClassName : 'DataTableWidget',
				initData : init
			});
			//roomListTbl.setRowFormatter(this.rowFormatter);
			roomListTbl.drawTable(this.rooms);
		}

		});
